package com.ll.homework;

import java.util.Calendar;
import java.util.Scanner;

/**
 * @author 刘龙
 * @version 1.0
 */
public class StringHomework01 {
    public static void main(String[] args) {
        String name = "jack";
        String pwd = "123456";
        String email = "ll@ll.com";
        try {
            userRegister(name, pwd, email);
            System.out.println("恭喜你注册成功");
        } catch (RuntimeException e) {
            System.out.println(e.getMessage());
        }
    }

    public static void userRegister(String name, String password, String email) {

        if (!(name != null && password != null && email != null)) {
            throw new RuntimeException("参数不能为空");
        }


        // 针对输入的内容进行校验
        // 用户名校验 用户名长度在2~4之间
        if (!(name.length() >= 2 && name.length() <= 4)) {
            throw new RuntimeException("用户名长度不对");
        }

        // 密码校验 密码长度是6位,并且全是数字
        if (!(password.length() == 6 && isDigital(password))) {
            throw new RuntimeException("密码的长度要为6,并且全是数字");
        }

        // 邮箱 @ 要在 . 之前
        // 找不到指定字符会返回-1
        int i = email.indexOf('@');
        int i2 = email.indexOf(".");
        if (!(i > 0 && i2 > i)) {
            throw new RuntimeException("邮箱格式错误");
        }
    }

    // 验证字符串是否由0~9组成
    public static boolean isDigital(String password) {
        char[] chars = password.toCharArray();
        for (char aChar : chars) {
            if (aChar < '0' || aChar > '9') {
                return false;
            }
        }
        return true;
    }
}
